package cn.dnui_cr503.dnuifood_16110100503.model;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.widget.Toast;

import java.util.List;

import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.bean.CollectStatus;

import cn.dnui_cr503.dnuifood_16110100503.listener.OnRetrofitListener;
import cn.dnui_cr503.dnuifood_16110100503.service.CollectionService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by chengrui on 2018/11/28.
 */

public class CollectionModel extends AppCompatActivity {
    private Context context;

    public CollectionModel(){

    }

    public void isCollect(String user_id, String shop_food_id, String flag, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175:8080/foodService/").addConverterFactory(GsonConverterFactory.create()).build();
        CollectionService collectService = retrofit.create(CollectionService.class);

        Call<CollectStatus> call = collectService.isCollected(user_id,shop_food_id,flag);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                // listener.onSuccess(response.body(),5);
                if (response.body().getCollected().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("已收藏");
                    // Toast.makeText(context,"收藏成功",Toast.LENGTH_SHORT).show();
                }else {
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                    //Toast.makeText(context,"取消收藏成功",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(CollectionModel.this,"网络连接失败",Toast.LENGTH_SHORT).show();
                //listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    public void userCollectShop(String user_id, String shop_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175:8080/foodService/").addConverterFactory(GsonConverterFactory.create()).build();
        CollectionService collectService = retrofit.create(CollectionService.class);

        Call<CollectStatus> call = collectService.userCollectShop(user_id,shop_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                //listener.onSuccess(response.body(),6);
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("已收藏");
                    // baseActivity.showToast("收藏成功");
                }else {
                    Toast.makeText(CollectionModel.this, "收藏店铺失败", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                // listener.onFail();
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);
    }

    public void unCollectShop(String user_id, String shop_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175:8080/foodService/").addConverterFactory(GsonConverterFactory.create()).build();
        CollectionService collectService = retrofit.create(CollectionService.class);

        Call<CollectStatus> call = collectService.userCollectShop(user_id,shop_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                    // baseActivity.showToast("取消收藏成功");

                }else {
                    //baseActivity.showToast("取消收藏失败");
                }

            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);
    }

    public void unCollectFood(String user_id, final String food_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectionService collectService = retrofit.create(CollectionService.class);
        Call<CollectStatus> call = collectService.userCollectFood(user_id,food_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                }else {
                    // Toast.makeText(context, "取消收藏店铺失败", Toast.LENGTH_SHORT).show();
                }

            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);

    }

    public void userCollectFood(String user_id, String food_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectionService collectService = retrofit.create(CollectionService.class);
        Call<CollectStatus> call = collectService.userCollectFood(user_id,food_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("已收藏");
                    // baseActivity.showToast("收藏成功");

                }else {
                    Toast.makeText(CollectionModel.this, "收藏店铺失败", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {

            }
        };

        call.enqueue(callback);
    }
    public void getAllUserCollection(String user_id,String flag,final OnRetrofitListener listener){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectionService collectService = retrofit.create(CollectionService.class);
        Call<List<CollectStatus>> call = collectService.getAllUserCollection(user_id,flag);
        Callback<List<CollectStatus>> callback = new Callback<List<CollectStatus>>() {
            @Override
            public void onResponse(Call<List<CollectStatus>> call, Response<List<CollectStatus>> response) {
                listener.onSuccess(response.body(),8);
            }
            @Override
            public void onFailure(Call<List<CollectStatus>> call, Throwable t) {
                Toast.makeText(context,"网络连接失败",Toast.LENGTH_SHORT).show();
                listener.onFailure("失败");
            }
        };
        call.enqueue(callback);
    }

}

